﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Unity;
using Skynet.BusinessEntities;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using Microsoft.Scripting;
namespace Skynet.BusinessLogic.Test.BLogic
{
    [TestClass]
    public class BCExamTest
    {
        public static IUnityContainer Container;
        [ClassInitialize()]
        public static void Initialize(TestContext testContext)
        {

            Container = UnityHelper.BuildUnityContainer();
        }
        [TestMethod]
        public void PythonExpressionEvaluator()
        {
            var rep = Container.Resolve<BusinessLogic.DataAccess.IAlternativeExpEvaluationRepository>();
            var key = new BusinessEntities.AlternativeExpEvaluation() { EvaluationKey = "test", EvaluationExp = "import System \r\nif Alternative.Answer == Alternative.CustomerAnswer : Score=5 \r\nelse : Score=0" };
            key.MarkAsDeleted();
            rep.Maintenance(key);
            key.MarkAsAdded();
            rep.Maintenance(key);
            var alt = new BoolAlternative() { Answer = true, CustomerAnswer = true, Score = 1 };
            var target = rep.GetOne("test");
            ScriptEngine engine = Python.CreateEngine();
            ScriptScope scope = engine.CreateScope();
            scope.SetVariable("Alternative", alt);
            ScriptSource source = engine.CreateScriptSourceFromString(target.EvaluationExp, SourceCodeKind.Statements);
            source.Execute(scope);
            var result =  scope.GetVariable<double>("Score");
            Assert.AreEqual<double>(1, result);
        }
    }
}
